package com.android.nearshop.app.activity.guesslike;

import java.util.List;

import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.android.nearshop.R;
import com.android.nearshop.app.activity.common.TemplateActivity;
import com.android.nearshop.app.activity.main.MainIndexActivity;
import com.android.nearshop.app.activity.search.SearchActivity;
import com.android.nearshop.app.adapter.listadapter.CommonListAdapter;
import com.android.nearshop.app.widget.PullToRefreshListView.OnRefreshListener;
import com.android.nearshop.app.widget.TitleBar;
import com.android.nearshop.app.widget.TitleBar.OnTitleClickListener;
import com.android.nearshop.business.location.MyBMapManager;
import com.android.nearshop.io.database.ShopDB;
import com.android.nearshop.io.protocol.in.BannerAction.ADV_TYPE;
import com.android.nearshop.io.protocol.in.FindShopAction;
import com.android.nearshop.io.protocol.out.Shop;

/**
 * @File: GuessLikeActivity.java
 * @Description: 猜你喜欢界面
 * @Author: denglixia
 * @Date: 2012-8-18
 * @Copyright : copyrights reserved by personal 2012-2022
 */
public class GuessLikeActivity extends TemplateActivity {

	private ListView mGuessLikeList = null;
	private CommonListAdapter mCommonListAdapter;
	private FindShopAction mAction;
	private TextView mTotalLabel;
	// private final double FIVE_KM_C = 0.045152d;
//	private Handler mUIHandler = new Handler() {
//		public void handleMessage(android.os.Message msg) {
//			Location loc = MyBMapManager.getInstace(GuessLikeActivity.this)
//					.getMyLoation();
//			List<Shop> shops = (List<Shop>) msg.obj;
//
//			mTotalLabel = (TextView) findViewById(R.id.total_label);
//			mTotalLabel.setText(String.format(getString(R.string.cheap_label,
//					"0")));
//			mCommonListAdapter.setTotalView(mTotalLabel);
//
//			if (shops == null || shops.size() == 0) {
//				mAction = new FindShopAction(loc.getLongitude(),
//						loc.getLatitude(), false, 1, 10);
//				mAction.setOrderStruct(true, false, false, false, false, false);
//				mCommonListAdapter.setAction(mAction);
//				mCommonListAdapter.loadData();
//			} else {
//				mCommonListAdapter.onLoaded(shops);
//				Shop shop = shops.get(0);
//				mTotalLabel.setText(shop.catalogyName
//						+ " "
//						+ String.format(getString(R.string.cheap_label,
//								shops.size())));
//			}
//
//		};
//	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_guesslike);
		mGuessLikeList = (ListView) findViewById(R.id.listView);
		mCommonListAdapter = new CommonListAdapter(this, mGuessLikeList);
		mCommonListAdapter.setIdisplayNetErrAndRefreshView(this);
		mGuessLikeList.setAdapter(mCommonListAdapter);
		mCommonListAdapter.setOnRefreshListener(new OnRefreshListener() {
			@Override
			public void onRefresh() {
				mCommonListAdapter.resetPageInfo();
				mCommonListAdapter.reload();
				loadData();
			}
		});
		findViewById(R.id.classic_button_panel).setVisibility(View.GONE);
		loadData();
	}

	private void loadData() {
		mCommonListAdapter.showLoading();
		Location loc = MyBMapManager.getInstace(GuessLikeActivity.this)
				.getMyGcjLoation();
		List<Shop> shops = ShopDB.findRangeShop(GuessLikeActivity.this, loc);

		mTotalLabel = (TextView) findViewById(R.id.total_label);
		mTotalLabel
				.setText(String.format(getString(R.string.cheap_label, "0")));
		mCommonListAdapter.setTotalView(mTotalLabel);

		if (shops == null || shops.size() == 0) {
			mAction = new FindShopAction(loc.getLongitude(), loc.getLatitude(),
					false, 1, 10);
			mAction.setOrderStruct(true, false, false, false, false, false);
			mCommonListAdapter.setScrollStateChangedEnble(true);
			mCommonListAdapter.setAction(mAction);
			mCommonListAdapter.loadData();
		} else {
			mCommonListAdapter.setScrollStateChangedEnble(false);
			mCommonListAdapter.onLoaded(shops);
			Shop shop = shops.get(0);
			mTotalLabel.setText(shop.catalogyName
					+ " "
					+ String.format(getString(R.string.cheap_label,
							shops.size())));
		}
	}

	@Override
	public void onBackPressed() {
		if (getParent() instanceof MainIndexActivity) {
			((MainIndexActivity) getParent()).onBackPressed();
		}
	}

	@Override
	protected void setupTitleBar(TitleBar titleBar) {
		titleBar.setTitle(R.string.tx_guslike);
		titleBar.setLeftImage(R.drawable.icon_shake_small_w);
		titleBar.setLeftBg(R.drawable.red_button_bg);
		titleBar.setRigthImage(R.drawable.search_normal);
		titleBar.setOnTitleClickListener(new OnTitleClickListener() {
			// 返回历史记录
			public void onRightClick() {
				Intent intent = new Intent();
				intent.setClass(getApplicationContext(), SearchActivity.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
				startActivity(intent);
			}

			// 返回主页面
			public void onLeftClick() {
				MainIndexActivity.gotoShake(GuessLikeActivity.this,
						mTitleBar.getLeftImgView());
			}
		}, true, true);
	}

	@Override
	protected ADV_TYPE getAdvType() {
		// 显示广告，如果打开且这页需要显示的话
		return ADV_TYPE.SUB;
	}
}
